package com.example.second;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.TextView;

import java.text.DecimalFormat;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    TextView weight;
    TextView height;
    Button maths;
    RadioButton boy;
    RadioButton girl;
    TextView BMI;
    TextView diagnose;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        weight=(TextView)findViewById(R.id.weight);
        height=(TextView)findViewById(R.id.height);
        maths=(Button)findViewById(R.id.maths);
        boy=(RadioButton)findViewById(R.id.iron_boy);
        girl=(RadioButton)findViewById(R.id.iron_girl);
        BMI=(TextView)findViewById(R.id.bmi);
        diagnose=(TextView)findViewById(R.id.diagnose);
        BMI.setText("");
        diagnose.setText("");
        maths.setOnClickListener((View.OnClickListener) this);
    }
    public void onClick(View v) {
        DecimalFormat nf = new DecimalFormat("0.00");  //保留两位小数点
        double Weight=Double.parseDouble(weight.getText().toString());
        double Height=Double.parseDouble(height.getText().toString());
        double result=0;
        if(Weight<=0 || Height<=0) {
            BMI.setText("值异常，不计算");
            diagnose.setText("值异常，不计算");
            return ;
        }
        result=(Weight)/(Height*Height);
        BMI.setText(nf.format(result));
        if(boy.isChecked()){
            if(result<20)
                diagnose.setText("体重过轻");
            else if(result>=20&&result<25)
                diagnose.setText("体重正常");
            else if(result>=25&&result<27)
                diagnose.setText("体重超重");
            else if(result>=27&&result<30)
                diagnose.setText("轻度肥胖");
            else if(result>=30&&result<35)
                diagnose.setText("中度肥胖");
            else if(result>=35)
                diagnose.setText("重度肥胖");
        }
        if(girl.isChecked()){
            if(result<19)
                diagnose.setText("体重过轻");
            else if(result>=19&&result<24)
                diagnose.setText("体重正常");
            else if(result>=24&&result<26)
                diagnose.setText("体重超重");
            else if(result>=26&&result<29)
                diagnose.setText("轻度肥胖");
            else if(result>=29&&result<34)
                diagnose.setText("中度肥胖");
            else if(result>=34)
                diagnose.setText("重度肥胖");
        }
    }
}
